package com.zzh.demo.core.enumeration;


import org.apache.commons.lang.StringUtils;

/**
 * description: <br>
 * createTime: 2019/2/149:16 <br>
 *
 * @author zzh
 */
public enum ErrorEnums {

    NO_SESSION(1001, "未登录"),
    LOGIN_ERROR(1002, "登录出错"),
    PARAM_REQUIRED(1003, "必填参数缺失") {
        @Override
        public String getTip(String... paramNames) {
            return msg + ": " + super.getTip(paramNames);
        }
    },
    PARAM_LENGTH_INVALID(1004, "参数长度无效") {
        @Override
        public String getTip(String... paramNames) {
            return super.getTip(paramNames) + msg;
        }
    },
    PARAM_PATTERN_INVALID(1005, "参数格式无效") {
        @Override
        public String getTip(String... paramNames) {
            if(paramNames != null && paramNames.length == 0 && StringUtils.isNotEmpty(paramNames[0])) {
                return paramNames[0];
            }
            return msg;
        }
    };

    protected int code;

    protected String msg;


    ErrorEnums(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public int getCode() {
        return code;
    }


    public String getMsg() {
        return msg;
    }

    public String getTip(String... paramNames) {
        if(paramNames == null || paramNames.length == 0) {
            return msg;
        } else {
            StringBuilder sb = new StringBuilder();
            for (String paramName : paramNames) {
                sb.append(paramName + ",");
            }
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        }
    }
}
